package com.wanmait.hotelManage.webservlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.hotelManage.dao.NewsCommentDAO;
import com.wanmait.hotelManage.dao.NewsDAO;
import com.wanmait.hotelManage.dao.RoomTypeCommentReportDAO;
import com.wanmait.hotelManage.vo.News;
import com.wanmait.hotelManage.vo.NewsComment;
import com.wanmait.hotelManage.vo.Pager;
import com.wanmait.hotelManage.vo.Userinfo;

/**
 * Servlet implementation class NewsListServlet
 */
@WebServlet("/NewsListServlet")
public class NewsListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("initTwo".equals(action)) {
			this.initTwo(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("de".equals(action)) {
			this.de(request, response);
		}else if("insert".equals(action)) {
			this.insert(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	Integer id=Integer.parseInt(request.getParameter("id"));
	Integer texti=Integer.parseInt(request.getParameter("texti"));
	String textare=request.getParameter("textare");
	HttpSession session=request.getSession();
	Userinfo userinfo=(Userinfo)session.getAttribute("userinfo");
	PrintWriter pw=response.getWriter();
	if(userinfo!=null) {
		System.out.println(userinfo.getId());
		NewsCommentDAO.getNewsCommentDAO().insertComment(textare,userinfo.getId(),id,texti);
		pw.print("ok");
	}else {
		session.setAttribute("goUrl", request.getHeader("Referer"));
		pw.print("no");
	}
	}
	/**
	 * 举报
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void de(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		String a=request.getParameter("a");
		HttpSession session=request.getSession();
		Userinfo userinfo=(Userinfo)session.getAttribute("userinfo");
		PrintWriter pw=response.getWriter();
		if(userinfo!=null) {
			NewsCommentDAO.getNewsCommentDAO().insert(id,userinfo.getId(),a);
			pw.print("ok");
		}else {
			session.setAttribute("goUrl", request.getHeader("Referer"));
			pw.print("no");
		}
	}
	/**
	 * 主题界面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		News ne=NewsDAO.getNewsDAO().findid(id);
		ArrayList<NewsComment>newsComment=NewsCommentDAO.getNewsCommentDAO().getFirstComment(id);
		Integer rows=NewsCommentDAO.getNewsCommentDAO().getrows(id);
		request.setAttribute("id", id);
		request.setAttribute("rows", rows);
		request.setAttribute("ne", ne);
		request.setAttribute("newsComment", newsComment);
		request.getRequestDispatcher("/newsDetail.jsp").forward(request, response);
	}
	/**
	 * 分页界面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void initTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer id=Integer.parseInt(request.getParameter("id"));
		System.out.println(id);
		Pager pager=new Pager();
		pager.setPageNo(id);
		pager.setPageSize(5);
		int rows=NewsDAO.getNewsDAO().getrows();
		pager.setPageCount(rows);
		List<News>news=NewsDAO.getNewsDAO().findall(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("news", news);
		request.getRequestDispatcher("/newsListTwo.jsp").forward(request, response);
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(5);
		int rows=NewsDAO.getNewsDAO().getrows();
		pager.setPageCount(rows);
		List<News>news=NewsDAO.getNewsDAO().findall(pager);
		request.setAttribute("pager", pager);
		request.setAttribute("news", news);
		request.getRequestDispatcher("/newsList.jsp").forward(request, response);
	}
}
